//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "LossDistMonteCarlo.h"
using namespace Cephei::QL::Experimental::Credit;
#include <gen/QL/Experimental/Credit/LossDist.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Credit::CLossDistMonteCarlo::CLossDistMonteCarlo (boost::shared_ptr<QuantLib::LossDistMonteCarlo>& childNative, Object^ owner) : CLossDist(CLossDistMonteCarlo::typeid)
{
#ifdef HANDLE
	_phLossDistMonteCarlo = NULL;
#endif
	_ppLossDistMonteCarlo = &childNative;
    _ppLossDist = new boost::shared_ptr<QuantLib::LossDist> (boost::dynamic_pointer_cast<QuantLib::LossDist> (*_ppLossDistMonteCarlo));
}
Cephei::QL::Experimental::Credit::CLossDistMonteCarlo::CLossDistMonteCarlo (QuantLib::LossDistMonteCarlo& childNative, Object^ owner) : CLossDist(CLossDistMonteCarlo::typeid)
{
#ifdef HANDLE
	_phLossDistMonteCarlo = NULL;
#endif
	_ppLossDistMonteCarlo = new boost::shared_ptr<QuantLib::LossDistMonteCarlo> (&childNative);
    _ppLossDist = new boost::shared_ptr<QuantLib::LossDist> (boost::dynamic_pointer_cast<QuantLib::LossDist> (*_ppLossDistMonteCarlo));
    _LossDistMonteCarloOwner = owner;
    _LossDistOwner = owner;
}

Cephei::QL::Experimental::Credit::CLossDistMonteCarlo::CLossDistMonteCarlo (CLossDistMonteCarlo^ copy) : CLossDist(CLossDistMonteCarlo::typeid)
{
#ifdef HANDLE
	_phLossDistMonteCarlo = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLossDistMonteCarlo = new boost::shared_ptr<QuantLib::LossDistMonteCarlo> (copy->GetShared());
        _ppLossDist = new boost::shared_ptr<QuantLib::LossDist> (boost::dynamic_pointer_cast<QuantLib::LossDist> (*_ppLossDistMonteCarlo));
    }
}
Cephei::QL::Experimental::Credit::CLossDistMonteCarlo::CLossDistMonteCarlo (System::Type^ t) : CLossDist(CLossDistMonteCarlo::typeid)
{
#ifdef HANDLE
	_phLossDistMonteCarlo = NULL;
#endif
	if (!t->IsSubclassOf(CLossDistMonteCarlo::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::CLossDistMonteCarlo::CLossDistMonteCarlo (QuantLib::Handle<QuantLib::LossDistMonteCarlo>& childNative, Object^ owner)  : CLossDist(CLossDistMonteCarlo::typeid)
{
	_phLossDistMonteCarlo = &childNative;
	_ppLossDistMonteCarlo = &static_cast<boost::shared_ptr<QuantLib::LossDistMonteCarlo>>(childNative.currentLink());
    _ppLossDist = new boost::shared_ptr<QuantLib::LossDist> (boost::dynamic_pointer_cast<QuantLib::LossDist> (*_ppLossDistMonteCarlo));
    _LossDistMonteCarloOwner = owner;
}
Cephei::QL::Experimental::Credit::CLossDistMonteCarlo::CLossDistMonteCarlo (QuantLib::Handle<QuantLib::LossDistMonteCarlo> childNative)  : CLossDist(CLossDistMonteCarlo::typeid)
{
	_phLossDistMonteCarlo = &childNative;
	_ppLossDistMonteCarlo = &static_cast<boost::shared_ptr<QuantLib::LossDistMonteCarlo>>(childNative.currentLink());
    _ppLossDist = new boost::shared_ptr<QuantLib::LossDist> (boost::dynamic_pointer_cast<QuantLib::LossDist> (*_ppLossDistMonteCarlo));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::CLossDistMonteCarlo::CLossDistMonteCarlo (QuantLib::LossDistMonteCarlo childNative)  : CLossDist(CLossDistMonteCarlo::typeid)
{
#ifdef HANDLE
	_phLossDistMonteCarlo = NULL;
#endif
	_ppLossDistMonteCarlo = new boost::shared_ptr<QuantLib::LossDistMonteCarlo> (new QuantLib::LossDistMonteCarlo (childNative));
    _ppLossDist = new boost::shared_ptr<QuantLib::LossDist> (boost::dynamic_pointer_cast<QuantLib::LossDist> (*_ppLossDistMonteCarlo));
}
#endif

Cephei::QL::Experimental::Credit::CLossDistMonteCarlo::~CLossDistMonteCarlo ()
{
    if (_ppLossDistMonteCarlo != NULL)
    {
	    delete _ppLossDistMonteCarlo;
        _ppLossDistMonteCarlo = NULL;
    }
}
Cephei::QL::Experimental::Credit::CLossDistMonteCarlo::!CLossDistMonteCarlo ()
{
    if (_ppLossDistMonteCarlo != NULL)
    {
	    delete _ppLossDistMonteCarlo;
    }
}
QuantLib::LossDistMonteCarlo& Cephei::QL::Experimental::Credit::CLossDistMonteCarlo::GetReference ()
{
    if (_ppLossDistMonteCarlo == NULL) throw gcnew NativeNullException ();
	return **_ppLossDistMonteCarlo;
}
boost::shared_ptr<QuantLib::LossDistMonteCarlo>& Cephei::QL::Experimental::Credit::CLossDistMonteCarlo::GetShared ()
{
    if (_ppLossDistMonteCarlo == NULL) throw gcnew NativeNullException ();
	return *_ppLossDistMonteCarlo;
}
QuantLib::LossDistMonteCarlo* Cephei::QL::Experimental::Credit::CLossDistMonteCarlo::GetPointer ()
{
    if (_ppLossDistMonteCarlo == NULL) throw gcnew NativeNullException ();
	return &**_ppLossDistMonteCarlo;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LossDistMonteCarlo>& Cephei::QL::Experimental::Credit::CLossDistMonteCarlo::GetHandle ()
{
	if (_phLossDistMonteCarlo == NULL)
	{
		_phLossDistMonteCarlo = new Handle<QuantLib::LossDistMonteCarlo> (*_ppLossDistMonteCarlo);
	}
	return *_phLossDistMonteCarlo;
}
#endif
bool Cephei::QL::Experimental::Credit::CLossDistMonteCarlo::HasNative () 
{
	return (_ppLossDistMonteCarlo != NULL);
}

UInt64 Cephei::QL::Experimental::Credit::CLossDistMonteCarlo::Buckets::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppLossDistMonteCarlo)->buckets ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CLossDistMonteCarlo::Maximum::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLossDistMonteCarlo)->maximum ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

